package com.pi.pro.xcmg.am.was.edi.mes.service;


import com.pi.config.MesWmsClientExchangeConfig;
import com.pi.pro.xcmg.am.common.vo.edi.ComponentItem;
import com.pi.pro.xcmg.am.was.edi.mes.fac.XPathSoapParser;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPMessage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;


/**
 */

@Service
public class MasterDataServiceProxy {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private XPathSoapParser xPathSoapParser;


    @Autowired
    private MesWmsClientExchangeConfig mesWmsClientExchangeConfig;


    public List<String> loadMaterials(){
        List<String> materialcodes=new ArrayList<>();

        String soapRequest = this.mesWmsClientExchangeConfig.materialRequestParams("202501171020");
        String response = this.call(this.mesWmsClientExchangeConfig.materialUrl(), soapRequest);

        List<ComponentItem> materials= null;
        try{
            materials = xPathSoapParser.parseMaterial(response);
        }catch (Exception e){
            //
        }
        return materialcodes;
    }

    public String call(String url, String soapRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_XML);
        headers.add("SOAPAction", "");

        HttpEntity<String> request = new HttpEntity<>(soapRequest, headers);
        return restTemplate.postForObject(url, request, String.class);
    }

    public static void parseSOAPMessage(String soapText) {
        try {
            // 创建SOAP工厂和消息工厂
            MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
            // 解析SOAP消息文件
            SOAPMessage soapMessage = messageFactory.createMessage(null, new FileInputStream(soapText));

            // 获取SOAP Body部分并转换为JAXB对象
            SOAPBody body = soapMessage.getSOAPBody();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //body.writeTo(baos); // 将SOAP Body转换为字节流
            String xmlContent = baos.toString("UTF-8"); // 将字节流转为字符串，以便JAXB处理

            // 使用JAXB解析XML内容到Java对象（假设有相应的类）
            JAXBContext context = JAXBContext.newInstance(ComponentItem.class); // YourClass是对应的Java类
            Unmarshaller unmarshaller = context.createUnmarshaller();
            ComponentItem obj = (ComponentItem) unmarshaller.unmarshal(new StringReader(xmlContent)); // 反序列化XML到Java对象
            System.out.println("Deserialized Object: " + obj); // 打印对象信息

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
